﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Synthetic.MicroApps.Framework;
using System.Reflection;

namespace Synthetic.MicroApps.Loader
{
    class Loader
    {
        public static IMAFramework[] GetPlugins(string directory)
        {
            if (String.IsNullOrEmpty(directory)) { return null; }

            DirectoryInfo info = new DirectoryInfo(directory);
            if (!info.Exists) { return null; }

            List<IMAFramework> plugins = new List<IMAFramework>();
            foreach (FileInfo file in info.GetFiles("*.dll"))
            {
                Assembly currentAssembly = Assembly.LoadFile(file.FullName);
                foreach (Type type in currentAssembly.GetTypes())
                {

                    if (!type.ImplementsInterface(typeof(IMAFramework)))
                    {
                        continue;
                    }
                    IMAFramework plugin = (IMAFramework)Activator.CreateInstance(type);
                    plugins.Add(plugin);
                }
            }

            return plugins.ToArray();
        }
    }
}
